a = int(input())
found = 0
hehehe = 0
if len(str(a)) != 4 or a <= 0:
   print("0")
else:
    aT = a // 1000
    aH = (a//100) % 10
    aTe = (a//10) % 10
    aU = a % 10
    wwfunny = [aT, aH, aTe, aU, a, (aT * 10 + aH), (aH * 10 + aTe), (aTe * 10 + aU), (aT * 100 + aH * 10 + aTe), (aH * 100 + aTe * 10 + aU)]
    while hehehe != len(wwfunny):
        k = 0
        for ok in range(2, wwfunny[hehehe] // 2 + 1):
            if (wwfunny[hehehe] % ok == 0):
                k = k + 1
        if (k <= 0):
            if found < wwfunny[hehehe]:
                found = wwfunny[hehehe]
        hehehe += 1
    if found == 1:
        print("0")
    else:
        print(found)